package Common 
{
	import com.greensock.TweenLite;
	import flash.display.Bitmap;
	import flash.display.CapsStyle;
	import flash.display.JointStyle;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.getDefinitionByName;
	import Fonts.CalibriEmbed;
	
	/**
	 * ...
	 * @author Serg de Adelantado
	 */
	public class LabelButton extends Sprite 
	{
		private const MIN_HEIGHT:int = 44;
		private var FIcon:Bitmap;
		private var FText:TextField;
		private var FFormat:TextFormat;
		private var FHighlite:Sprite;
		
		public function LabelButton(AIcon:String, AText:String)
		{
			var vClass:Class = getDefinitionByName(AIcon) as Class;
			var vFont:CalibriEmbed = new CalibriEmbed();
			FFormat = new TextFormat();
			FFormat.font = vFont.fontName;
			FFormat.size = 14;
			FIcon = new Bitmap(new vClass());
			FText = new TextField();
			FText.defaultTextFormat = FFormat;
			FText.text = AText;	
			FText.width = FText.textWidth + 5;
			FText.height = FText.textHeight + 4;
			FText.selectable = mouseChildren = false;
			useHandCursor = buttonMode = true;
			var vHeight:int = Math.max(Math.round(FIcon.y + FIcon.height + 10), MIN_HEIGHT);
			FText.x = FIcon.width;
			FText.y = (vHeight - FText.height) / 2 - 5;
			FIcon.y = (vHeight - FIcon.height) / 2 - 5;
			graphics.beginFill(0xEEEEEE, 0.75);
			graphics.lineStyle(1, 0, 0.75, true, 'normal', CapsStyle.SQUARE, JointStyle.ROUND);
			graphics.drawRoundRect( -5, -5, FText.x + FText.width + 10, vHeight, 10);
			graphics.endFill();			
			FHighlite = new Sprite();
			FHighlite.graphics.beginFill(0xFFFFFF, 1);			
			FHighlite.graphics.lineStyle(1, 0, 0.75, true, 'normal', CapsStyle.SQUARE, JointStyle.ROUND);
			FHighlite.graphics.drawRoundRect( -5, -5, FText.x + FText.width + 10, vHeight, 10);
			FHighlite.alpha = 0;					
			addChild(FHighlite);
			addChild(FIcon);
			addChild(FText);
			filters = [new DropShadowFilter(0, 0, 0, 0.5, 5, 5, 1, 3)];
			addEventListener(MouseEvent.MOUSE_OVER, OnMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, OnMouseOut);			
		}
				
		protected function OnMouseOver(e:MouseEvent):void 
		{
			TweenLite.to(FHighlite, 0.5, { alpha: 1 } );			
		}
		
		protected function OnMouseOut(e:MouseEvent):void 
		{
			TweenLite.to(FHighlite, 0.5, { alpha: 0 } );			
		}
	}
}